/*************************************************************************
	> File Name: devices.cpp
	> Author: xuwenlong
	> Mail: myxuan475@126.com 
	> Created Time: 2018年04月12日 星期四 15时01分12秒
 ************************************************************************/

#include "devices.h"
#include <stdio.h>
#include <stdlib.h>
#include "transports.h"

devices::devices(int id):
    dbms("das.db","devices",id) {

}

devices::~devices()
{
}

BOOL devices::inorup(const string &name, const string &devid, S32 model, S32 type, S32 transId)
{
    if (!exist())
        return insert(name,devid,model,type,transId);
    else
        return update(name,devid,model,type,transId);
}

BOOL devices::insert(const string& name,const string &devid,S32 model, S32 type, S32 transId)
{
    char cond[MAX_COND_LEN] = {0};

    sprintf(cond,"%d,'%s','%s',%d,%d,%d",m_iId,devid.c_str(),name.c_str(),model,type,transId);
    return m_sqlite.Insert("id,devId,name,model,type,tranId",cond);
}

BOOL devices::update(const string &name, const string &devid, S32 model, S32 type, S32 transId)
{
    char cond[MAX_COND_LEN] = {0};

    sprintf(cond,"devId='%s',name='%s',model=%d,type=%d,tranId=%d",devid.c_str(),name.c_str(),model,type,transId);
    return m_sqlite.Update(cond,getcondition().c_str());
}

void devices::assignvalue(int argc, char **argv, char **azColName)
{
#define STR_EQUAL(s1,s2) !strcmp(s1,s2)
    deviceinfo_t dev;
    for (int i = 0;i < argc;i++) {
        if (argv[i] == NULL)
            return;
        if (STR_EQUAL(azColName[i],"id")) {
            dev.id = atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"devId")) {
            dev.dev_id = argv[i];
        }
        else if (STR_EQUAL(azColName[i],"name")) {
            dev.name = argv[i];
        }
        else if (STR_EQUAL(azColName[i],"model")) {
            dev.model_id = atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"type")) {
            dev.dev_type = atoi(argv[i]);
        }
        else if (STR_EQUAL(azColName[i],"tranId")) {
            dev.tran_id = atoi(argv[i]);
        }
    }
    m_devices.push_back(dev);
}

void devices::clearall()
{
    m_devices.clear();
}

vector<deviceinfo_t> devices::getdevices_by_chn(const string &chn)
{
    if (chn == "tcp")
        FetchItems("devices d,transport t","d.*","t.mode=0 order by d.type");
    else
        FetchItems("devices d,transport t","d.*","d.tranId=t.id and t.mode=1 and t.addr like '%"+chn+"%' order by d.type");

    for (vector<deviceinfo_t>::iterator iter = m_devices.begin();
         iter != m_devices.end();iter++) {
        deviceinfo_t &dev = *iter;
        transports tran(dev.tran_id);
        tran.FetchItems();
        dev.trans = tran.gettransinfo();
    }
    return m_devices;
}

vector<deviceinfo_t> &devices::getdevices()
{
    for (vector<deviceinfo_t>::iterator iter = m_devices.begin();
         iter != m_devices.end();iter++) {
        deviceinfo_t &dev = *iter;
        transports tran(dev.tran_id);
        tran.FetchItems();
        dev.trans = tran.gettransinfo();
    }
    return m_devices;
}

deviceinfo_t devices::getdevice()
{
    if (m_devices.size() > 0) {
        deviceinfo_t& dev = m_devices.at(0);
        transports tran(dev.tran_id);
        tran.FetchItems();
        dev.trans = tran.gettransinfo();
        return dev;
    }
    deviceinfo_t dev;
    return dev;
}

